<template>
  <div>
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="form">
      <el-form-item prop="roleName">
        <el-input v-model="dataForm.roleName" placeholder="角色名称"
                  maxlength="10" class="input" clearable="clearable"></el-input>
      </el-form-item>
      <el-form-item prop="desc">
        <el-input v-model="dataForm.desc" placeholder="备注"
                  maxlength="10" class="input" clearable="clearable"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="dataForm.systemic" placeholder="是否是系统配置"
                  maxlength="10" class="input" clearable="clearable"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchHandle()">查询</el-button>
        <el-button type="primary"
                   :disabled="!proxy.isAuth(['ROOT','ROLE:INSERT'])"
                   @click="addHandle()">
          新增
        </el-button>
        <el-button type="danger"
                   :disabled="!proxy.isAuth(['ROOT','ROLE:DELETE'])"
                   @click="deleteHandle()">
          批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table :data="data.dataList"
              :headel-cell-style="{'background':'#f5f7fa'}" border
              v-loading="data.loading" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center"
                       :selectable="selectable" align="center" width="50"/>
      <el-table-column type="index" header-align="center" align="center"
                       width="100" label="序号">
        <template #default="scope">
          <span>{{ (data.pageIndex - 1) * data.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="roleName" header-align="center" align="center"
                       label="角色名称" min-width="50"/>
      <el-table-column prop="permissions" header-align="center" align="center"
                       label="权限集合" min-width="50"/>
      <el-table-column prop="defaultPermissions" header-align="center" align="center"
                       label="系统角色内置权限" min-width="50"/>
      <el-table-column prop="desc" header-align="center" align="center"
                       label="描述" min-width="50"/>
      <el-table-column prop="systemic" header-align="center" align="center"
                       label="是否是系统内置角色" min-width="50"/>
      <el-table-column header-align="center" align="center" width="150" label="操作">
        <template #default="scope">
          <el-button type="text"
                     v-if="proxy.isAuth(['ROOT','ROLE:UPDATE'])"
                     @click="updateHandle(scope.row.id)">
            修改
          </el-button>
          <el-button type="text" :disabled="scope.row.root"
                     v-if="proxy.isAuth(['ROOT','ROLE:DELETE'])"
                     @click="deleteHandle(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="data.pageIndex"
        :page-size="data.pageSize"
        :page-sizes="[10, 20, 50]"
        :total="data.totalCount"
        layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
  <el-dialog
      :title="!dialog.dataForm.roleId ? '新增' : '修改'"
      :close-on-click-modal="false"
      v-model="dialog.visible"
      width="450px"
  >
    <el-form
        :model="dialog.dataForm"
        ref="dialogForm"
        :rules="dialog.dataRule"
        label-width="80px"
    >
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="dialog.dataForm.roleName" maxlength="20" clearable/>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="dialog.dataForm.desc" maxlength="200" clearable/>
      </el-form-item>
      <el-form-item label="系统内置角色" prop="systemic">
        <el-input v-model="dialog.dataForm.systemic" maxlength="200" clearable/>
      </el-form-item>
    </el-form>
    <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialog.visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit">确定</el-button>
    </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {getCurrentInstance, reactive} from "vue";

const {proxy} = getCurrentInstance();
const dataForm = reactive({
  roleName: '',
  permissions: '',
  defaultPermissions: '',
  desc: '',
  systemic: ''
});
const data = reactive({
  dataList: [],
  pageIndex: 1,
  pageSize: 10,
  totalCount: 0,
  loading: false,
  selections: [],
})

const dialog = reactive({
  visible: false,
  update: false,
  dataForm: {
    roleId: null,
    roleName: null,
    desc: null,
    systemic: null
  },
  dataRule: {
    roleName: [
      {required: true, message: '名称不能为空'},
    ],
    systemic: [
      {required: true, message: 'systemic不能为空'},
    ],
  }
})

function loadDataList() {
  data.loading = true;
  let json = {
    page: data.pageIndex,
    length: data.pageSize,
    roleName: dataForm.roleName,
    systemic: dataForm.systemic,
    desc: dataForm.desc
  };
  proxy.$http("/mis/role/searchByPage", 'POST', json, true, function (resp) {
    let page = resp.page;
    let list = page.list;
    for (let one of list) {
      if (one.systemic = true) {
        one.systemic = '是'
      } else {
        one.systemic = '否'
      }
    }
    data.dataList = list;
    data.totalCount = page.totalCount;
    data.loading = false;
  })
}

function searchHandle() {
  loadDataList();
}

function addHandle() {
  dialog.dataForm.id = null;
  dialog.update = false;
  dialog.visible = true;
  proxy.$nextTick(function () {
    proxy.$refs['dialogForm'].resetFields();
  })
}

function dataFormSubmit() {
  proxy.$refs['dialogForm'].validate((valid) => {
        if (valid) {
          let json = {
            roleId: dialog.dataForm.roleId,
            roleName: dialog.dataForm.roleName,
            desc: dialog.dataForm.desc,
            systemic: dialog.dataForm.systemic === true ? 1 : '0'
          }
          proxy.$http(`/mis/role/${dialog.dataForm.roleId == null ? 'insert' : 'update'}`, 'POST', json, true, function (resp) {
                if (resp.rows == 1) {
                  proxy.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1200,
                    onClose: () => {
                      dialog.visible = false;
                      loadDataList();
                    }
                  })

                } else {
                  proxy.$message({
                    message: '操作失败',
                    type: 'error',
                    duration: 1200,
                    onClose: () => {
                      dialog.visible = false;
                    }
                  })
                }
                dialog.visible = false;
              }
          )
        }
      }
  )
}

function sizeChangeHandle(val) {
  data.pageSize = val;
  loadDataList();
}

function updateHandle(id) {
  dialog.update = true
  dialog.visible = true;
  proxy.$nextTick(() => {
    let json = {
      roleId: id
    };
    proxy.$http('/mis/role/searchById', 'POST', json, true, function (resp) {
      let result = resp.result;
      dialog.dataForm.roleId = result.id;
      dialog.dataForm.roleName = result.roleName;
      dialog.dataForm.desc = result.desc;
      dialog.dataForm.systemic = result.systemic
    })
  })
}

function currentChangeHandle(val) {
  data.pageIndex = val;
  loadDataList();
}

function deleteHandle(id) {
  let ids = id ? [id] : data.selections.map(item => {
    return item.id;
  })
  if (ids.length == 0) {
    proxy.$message({
      message: '没有选中记录',
      type: 'warning',
      duration: 1200
    });
  } else {
    proxy.$confirm('确认要删除选中的记录?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'waring'
    })
        .then(() => {
          proxy.$http('/mis/role/deleteByIds', 'POST', {ids: ids}, true, function (resp) {
            if (resp.rows > 0) {
              proxy.$message({
                message: '操作成功',
                type: 'success',
                duration: 1200,
                onClose: () => {
                  loadDataList();
                }
              })
            } else {
              proxy.$message({
                message: '未能删除记录',
                type: 'warning',
                duration: 1200
              })
            }
          })
        })
  }
}

function selectionChangeHandle(val) {
  data.selections = val;
}

loadDataList();
</script>

<style scoped lang="less">

</style>
